﻿using System;
using System.Data;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using log4net;
namespace AnyOffice.general.system.dept
{
    public partial class dept_edit : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        protected string strTemp;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int num = (Request.QueryString["dept_id"] == null) ? 0 : int.Parse(Request.QueryString["dept_id"].ToString());
                this.ViewState["DeptId"] = num;
                DepartmentDao dao = new DepartmentDao();
                DataTable table = dao.SelectByDeptId(base.Conn, num);
                if (table.Rows.Count == 0)
                {
                    this.pnlMain.Visible = false;
                    this.lblMessage.Text = base.ShowErrorNoNav("没有相关部门/成员单位！");
                    return;
                }
                this.lblDeptName.Text = (table.Rows[0]["DeptName"] != DBNull.Value) ? table.Rows[0]["DeptName"].ToString() : "";
                this.txtDeptNo.Text = (table.Rows[0]["DeptNo"] != DBNull.Value) ? table.Rows[0]["DeptNo"].ToString() : "";
                this.txtDeptName.Text = (table.Rows[0]["DeptName"] != DBNull.Value) ? table.Rows[0]["DeptName"].ToString() : "";
                this.txtTelNo.Text = (table.Rows[0]["TelNo"] != DBNull.Value) ? table.Rows[0]["TelNo"].ToString() : "";
                this.txtFaxNo.Text = (table.Rows[0]["FaxNo"] != DBNull.Value) ? table.Rows[0]["FaxNo"].ToString() : "";
                this.txtDeptFunc.Text = (table.Rows[0]["DeptFunc"] != DBNull.Value) ? table.Rows[0]["DeptFunc"].ToString() : "";
                if (table.Rows[0]["Manager"] != DBNull.Value)
                {
                    this.ManagerId.Value = table.Rows[0]["Manager"].ToString();
                }
                if ((table.Rows[0]["ManagerName"] != DBNull.Value) && (table.Rows[0]["ManagerName"].ToString() != string.Empty))
                {
                    this.txtManagerName.Text = table.Rows[0]["ManagerName"].ToString() + ",";
                }
                if (table.Rows[0]["Leader1"] != DBNull.Value)
                {
                    this.Leader1Id.Value = table.Rows[0]["Leader1"].ToString();
                }
                if ((table.Rows[0]["Leader1Name"] != DBNull.Value) && (table.Rows[0]["Leader1Name"].ToString() != string.Empty))
                {
                    this.txtLeader1Name.Text = table.Rows[0]["Leader1Name"].ToString() + ",";
                }
                if (table.Rows[0]["Leader2"] != DBNull.Value)
                {
                    this.Leader2Id.Value = table.Rows[0]["Leader2"].ToString();
                }
                if ((table.Rows[0]["Leader2Name"] != DBNull.Value) && (table.Rows[0]["Leader2Name"].ToString() != string.Empty))
                {
                    this.txtLeader2Name.Text = table.Rows[0]["Leader2Name"].ToString() + ",";
                }
                DataTable table2 = dao.SelectParentAll(base.Conn);
                ListItem item = new ListItem("无", "0");
                this.ddlParentDept.Items.Add(item);
                if (table2.Rows.Count > 0)
                {
                    for (int i = 0; i < table2.Rows.Count; i++)
                    {
                        if (int.Parse(table2.Rows[i]["DeptId"].ToString()) != num)
                        {
                            item = new ListItem("|─" + table2.Rows[i]["DeptName"].ToString(), table2.Rows[i]["DeptId"].ToString());
                            this.ddlParentDept.Items.Add(item);
                            this.strTemp = "";
                            this.GetDept(ref this.ddlParentDept, int.Parse(table2.Rows[i]["DeptId"].ToString()), this.strTemp);
                        }
                    }
                }
                if (this.ddlParentDept.Items.FindByValue(table.Rows[0]["ParentDept"].ToString()) != null)
                {
                    this.ddlParentDept.Items.FindByValue(table.Rows[0]["ParentDept"].ToString()).Selected = true;
                }
            }
            if (this.Page.IsPostBack)
            {
                DepartmentDao dao2 = new DepartmentDao();
                DepartmentData data = new DepartmentData();
                data.DeptId = Convert.ToInt32(this.ViewState["DeptId"].ToString());
                data.DeptNo = Convert.ToInt32(this.txtDeptNo.Text);
                data.DeptName = this.txtDeptName.Text;
                data.TelNo = this.txtTelNo.Text;
                data.FaxNo = this.txtFaxNo.Text;
                data.ParentDept = Convert.ToInt32(this.ddlParentDept.SelectedValue);
                if (this.ManagerId.Value != string.Empty)
                {
                    data.Manager = this.ManagerId.Value;
                }
                if (this.Leader1Id.Value != string.Empty)
                {
                    data.Leader1 = this.Leader1Id.Value;
                }
                if (this.Leader2Id.Value != string.Empty)
                {
                    data.Leader2 = this.Leader2Id.Value;
                }
                data.DeptFunc = this.txtDeptFunc.Text;
                try
                {
                    if (dao2.Update(base.Conn, data) == -1)
                    {
                        this.litDeptName.Text = "相同名称的同级部门已经存在";
                        return;
                    }
                    string str = this.txtDeptName.Text + ",DeptId=" + this.ViewState["DeptId"].ToString();
                    SysLog.Log(base.Conn, 4, str);
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                    this.pnlMain.Visible = false;
                    this.lblMessage.Text = base.ShowErrorNoNav("部门/成员单位修改失败！");
                    return;
                }
                this.pnlMain.Visible = false;
                this.lblMessage.Text = base.ShowPromptNoNav("部门/成员单位修改成功！");
                StringBuilder builder = new StringBuilder("<script>\n");
                builder.Append("parent.dept_list.location.replace('dept_list.aspx');\n");
                builder.Append("</script>\n");
                if (!this.Page.IsClientScriptBlockRegistered("dept_list"))
                {
                    this.Page.RegisterClientScriptBlock("dept_list", builder.ToString());
                }
            }
        }
        private void GetDept(ref DropDownList ddlParentDept, int DeptId, string strTemp)
        {
            int num = Convert.ToInt32(this.ViewState["DeptId"].ToString());
            DataTable table = new DepartmentDao().SelectChildAll(base.Conn, DeptId);
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "|　";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    if (int.Parse(table.Rows[i]["DeptId"].ToString()) != num)
                    {
                        ListItem item = new ListItem(strTemp + "|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                        ddlParentDept.Items.Add(item);
                        this.GetDept(ref ddlParentDept, int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp);
                    }
                }
            }
        }
    }
}
